/*
 * 文件名：LoginFilter.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2013-4-11
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.manager.action;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wootide.cms.common.manager.Constants;
import com.wootide.cms.manager.pojo.User;	

public class LoginFilter extends HttpServlet implements Filter {

	/**
	 * 意义，目的和功能，以及被用到的地方<br>
	 */
	private static final long serialVersionUID = -3719061865662109670L;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		HttpServletResponse response = (HttpServletResponse) arg1;
		HttpServletRequest request = (HttpServletRequest) arg0;
		HttpSession session = request.getSession(true);
		//String usercode = (String) session.getAttribute("usercode");//    
		User usercode = (User) session.getAttribute(Constants.LOGIN_USER);
		String uri = request.getRequestURI();

		String path = request.getContextPath();
	    String basePath = request.getScheme() + "://"
	            + request.getServerName() + ":" + request.getServerPort()
	            + path + "/"; 
		
		//用户是否已经登录
		if (usercode == null || usercode.equals("")) {
			// 判断获取的路径不为空且不是访问登录页面或执行登录操作时跳转
			if (uri != null && !uri.equals("")
					&& (uri.indexOf("Login") < 0 && uri.indexOf("login") < 0)) {
				 response.sendRedirect(basePath+"login.jsp");
				//request.getRequestDispatcher("login.jsp").forward(arg0, arg1);
				return;
			}
		}
		// 已通过验证，用户访问继续
		arg2.doFilter(arg0, arg1);
		return;
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

	/*
	 * public void doFilter(ServletRequest sRequest, ServletResponse sResponse,
	 * FilterChain filterChain) throws IOException, ServletException {
	 * 
	 * HttpServletRequest request = (HttpServletRequest) sRequest;
	 * HttpServletResponse response = (HttpServletResponse) sResponse;
	 * HttpSession session = request.getSession(); String url =
	 * request.getServletPath(); String contextPath = request.getContextPath();
	 * if (url.equals("")) url += "/"; if ((url.startsWith("/") &&
	 * !url.startsWith("/login"))) {// 若访问后台资源 // 过滤到login User loginUser =
	 * (User) session.getAttribute(Constants.LOGIN_USER); if (loginUser == null)
	 * {// 转入管理员登陆页面
	 * 
	 * } } filterChain.doFilter(sRequest, sResponse); }
	 */

	/*
	 * public void doFilter1(ServletRequest request, ServletResponse response,
	 * FilterChain filterChain) throws IOException, ServletException {
	 * 
	 * HttpServletRequest httpReq = (HttpServletRequest) request;
	 * HttpServletResponse hresponse = (HttpServletResponse) response; String
	 * uri = httpReq.getRequestURI(); HttpSession ss = httpReq.getSession();
	 * User loginUser = (User) ss.getAttribute(Constants.LOGIN_USER);
	 * 
	 * if (uri.indexOf(".do") > -1 || uri.indexOf(".jsp") > -1 ||
	 * uri.indexOf(".html") > -1) { if (loginUser == null) { //
	 * hresponse.sendRedirect("tologin.action"); if (uri.indexOf("login.jsp") >
	 * -1 || uri.indexOf("login.do") > -1) { //filterChain.doFilter(request,
	 * response); } else if (uri.indexOf("logout.do") > -1) {
	 * filterChain.doFilter(request, response); } else {
	 * 
	 * }
	 * 
	 * }else{
	 * 
	 * } filterChain.doFilter(request, response); }
	 * filterChain.doFilter(request, response); }
	 */

}
